/*
 * The MIT License (MIT)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package multiswarm;

import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.Manifold;

/**
 * AgentContactListener is our custom contact listener so that we can update sensors.
 * @author ralfm
 */
public class AgentContactListener implements ContactListener {

	@Override
	public void beginContact(Contact contact) {
		//Gdx.app.log("ContactListener", "BeginContact()");
		Fixture fixtureA = contact.getFixtureA();
		Fixture fixtureB = contact.getFixtureB();
		
		if (fixtureA.isSensor() ^ fixtureB.isSensor()) {
			AgentSensor sensor = null;
			Agent agent = null;
			
			if (fixtureA.isSensor() && !fixtureB.isSensor()) {
				sensor = (AgentSensor)fixtureA.getUserData();
				agent = (Agent)fixtureB.getUserData();
			} else if (!fixtureA.isSensor() && fixtureB.isSensor()) {
				sensor = (AgentSensor)fixtureB.getUserData();
				agent = (Agent)fixtureA.getUserData();
			}
			
			sensor.agentEntered(agent);
		} else {
			//both are bodies, handle collision...
		}
	}

	@Override
	public void endContact(Contact contact) {
		//Gdx.app.log("ContactListener", "EndContact()");
		Fixture fixtureA = contact.getFixtureA();
		Fixture fixtureB = contact.getFixtureB();
		
		if (fixtureA.isSensor() ^ fixtureB.isSensor()) {
			AgentSensor sensor = null;
			Agent agent = null;
			
			if (fixtureA.isSensor() && !fixtureB.isSensor()) {
				sensor = (AgentSensor)fixtureA.getUserData();
				agent = (Agent)fixtureB.getUserData();
			} else if (!fixtureA.isSensor() && fixtureB.isSensor()) {
				sensor = (AgentSensor)fixtureB.getUserData();
				agent = (Agent)fixtureA.getUserData();
			}
			
			sensor.agentExitted(agent);
		} else {
			//both are bodies, handle collision...
			
		}		
	}

	@Override
	public void preSolve(Contact contact, Manifold oldManifold) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void postSolve(Contact contact, ContactImpulse impulse) {
		// TODO Auto-generated method stub
		
	}

}
